var express = require('express');
var router = express.Router();
const { Course,Category,User } = require('../models');
// const { Op } = require('sequelize');

// const { NotFoundError } = require('../utils/errors');
const { success, failure } = require('../utils/responses');
const { where } = require('sequelize');

/* GET home page. */
router.get('/', async function(req, res, next) {
    try {
    // 推荐课
    const recommendedCourses = await Course.findAll({
        attributes: { exclude: ['CategoryId', 'UserId', 'content'] },
        include: [
            { model: Category, as: 'category', attributes: ['id', 'name'] },
            { model: User, as: 'user', attributes: ['id', 'username', 'avatar','company'] },
        ],
        order: [['id', 'DESC']],
        where: {recommended: true},
        limit: 10
    })
        
    // 人气课
    const likesCourses = await Course.findAll({
        attributes: { exclude: ['CategoryId', 'UserId', 'content'] },
        order: [['likesCount', 'DESC']],
        limit: 10
    })
    // 入门课程
    const introductoryCourses = await Course.findAll({
        attributes: { exclude: ['CategoryId', 'UserId', 'content'] },
        where: { introductory: true },
        order: [['likesCount', 'DESC']],
        limit: 10
    })
    success(
        res, '获取首页数据成功',
        {recommendedCourses, likesCourses, introductoryCourses}
    )
  } catch (error) {
    
  }
});

module.exports = router;
